// pages/outvisit/task/inputInfo.js
var api = require("../../../utils/api.js");
var common = require("../../../utils/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pickerValue: [0],//下拉框的值
    id: 0,//任务ID
    returnTypeList: common.returnTypeList,//退回类型
    outvistRecord: {},//外访记录
    appointdate: "",//外访日期
    isNoUpdate: true,//是否不允许更新
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;

    // 设置任务ID
    that.setData({
      id: id
    }, () => {
      console.log("id===============>"+id);
      // 获取外访记录
      //that.getOutvistRecord();
    });
  },

  // 设置表单下拉框默认值
  setPickerValue: function (data) {
    var that = this;
    var pickerValue = that.data.pickerValue;

    console.log(data.returnTypeList);
    if (data.returnTypeList) {
      pickerValue[0] = data.returnTypeList;
    }
    that.setData({
      pickerValue: pickerValue
    });
  },

  // 提交表单
  formSubmit: function (e) {
    var that = this;
    if (that.data.isNoUpdate){
      wx.showModal({
        title: "提示",
        content: "确定要提交吗？",
        success: function (sm) {
          if (sm.confirm) {
            // 提交表单数据
            that.formSubmitData(e);
          }
        }
      })
    } else {
      // 提交表单数据
      that.formSubmitData(e);
    }
  },

  // 提交表单数据
  formSubmitData: function (e) {
    var that = this;
    var data = e.detail.value;
    var submitData = {};
    for (var d in data) {
      submitData[d] = data[d];
    }
    console.log(submitData);

    if (submitData.appointdate == '') {
      wx.showToast({
        title: "请选择预约时间",
        icon: "none"
      });
      return;
    }

    wx.showLoading({
      title: '提交中'
    });//显示加载中
    api.makeSubmitAppointforApp(submitData).then(res => {
      if (res.code == 0) {
        wx.hideLoading();

        let pages = getCurrentPages();//当前页面
        console.log(pages);
        let prevPage = pages[pages.length - 3];//上一页面
        prevPage.getAppointmentList();

        wx.navigateBack({
          delta: 3,
          success: function () {
            wx.showToast({
              title: "提交成功",
              icon: "none"
            });
          }
        });
      }
    });
  },


  //时间选择
  bindDateChange(e) {
    let that = this;
    that.setData({
      appointdate: e.detail.value
    })
  },

  bindTimeChange: function(e) {
    this.setData({
      appointtime: e.detail.value
    })
  },

  //下拉框选择事件
  bindPickerChange: function (e) {
    let pickerValue = this.data.pickerValue;
    pickerValue[parseInt(e.currentTarget.dataset.id)] = e.detail.value;
    this.setData({
      pickerValue: pickerValue
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
